package controller;

import java.io.IOException;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.UnknownHostException;

import java.sql.SQLException;
import java.sql.Statement;

import dataLayer.DBconnect;

public class ConnectionAvailable extends DBconnect {

	private static ConnectionAvailable single;

	public boolean isInternetReachable() {
		try {

			// make a URL to a known source
			URL url = new URL("http://www.martcor.nl");

			// open a connection to that source
			HttpURLConnection urlConnect = (HttpURLConnection) url
					.openConnection();

			// trying to retrieve data from the source. If there
			// is no connection, this line will fail
			@SuppressWarnings("unused")
			Object objData = urlConnect.getContent();
			openConn();
			Statement stmt = getConn().createStatement();
			String sql = "select 1";
			stmt.execute(sql);
			stmt.close();
			closeConn();
			return true;
		} catch (UnknownHostException e) {			
			return false;
		} catch (IOException e) {			
			return false;
		} catch (SQLException e) {			
			return false;
		}
	}

	public static ConnectionAvailable getInstance() {
		if (single == null) {
			single = new ConnectionAvailable();
		}
		return single;

	}

}
